﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:SysCon
    /// </summary>
    public partial class SysCon
    {
        public SysCon()
        {
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.SysCon model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into SysCon(");
            strSql.Append("Name,Stats,number)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Stats,@number)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Name", SqlDbType.VarChar, 80),
                    new SqlParameter("@Stats", SqlDbType.Int, 4),
                    new SqlParameter("@number", SqlDbType.Int, 4) };
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Stats;
            parameters[2].Value = model.number;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.SysCon model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update SysCon set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Stats=@Stats,");
            strSql.Append("number=@number");
            strSql.Append(" where Order=@Order");
            var parameters = new SqlParameter[] { new SqlParameter("@Name", SqlDbType.VarChar, 80),
                    new SqlParameter("@Stats", SqlDbType.Int, 4),
                    new SqlParameter("@number", SqlDbType.Int, 4),
                    new SqlParameter("@Order", SqlDbType.Int, 4) };
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Stats;
            parameters[2].Value = model.number;
            parameters[3].Value = model.Order;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Order)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from SysCon ");
            strSql.Append(" where Order=@Order");
            var parameters = new SqlParameter[] { new SqlParameter("@Order", SqlDbType.Int, 4)
            };
            parameters[0].Value = Order;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Orderlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from SysCon ");
            strSql.Append(" where Order in (" + Orderlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.SysCon GetModel(int Order)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 Order,Name,Stats,number from SysCon ");
            strSql.Append(" where Order=@Order");
            var parameters = new SqlParameter[] { new SqlParameter("@Order", SqlDbType.Int, 4)
            };
            parameters[0].Value = Order;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.SysCon DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.SysCon();
            if (row != null)
            {
                if (row["Order"] != null && row["Order"].ToString() != string.Empty)
                {
                    model.Order = int.Parse(row["Order"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Stats"] != null && row["Stats"].ToString() != string.Empty)
                {
                    model.Stats = int.Parse(row["Stats"].ToString());
                }
                if (row["number"] != null && row["number"].ToString() != string.Empty)
                {
                    model.number = int.Parse(row["number"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Order,Name,Stats,number ");
            strSql.Append(" FROM SysCon ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Order,Name,Stats,number ");
            strSql.Append(" FROM SysCon ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SysCon ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.Order desc");
            }
            strSql.Append(")AS Row, T.*  from SysCon T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

